﻿using System;
using System.Collections.Generic;
using System.Text;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.Core.Calculations;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Manipulation
{
    public class CommandFixSelectedShprossConnectorAngles : SerializedUndoRedo, ICommand, IModelVisitor
    {
        public CommandFixSelectedShprossConnectorAngles(ModelControl modelControl)
        {
            this.modelControl = modelControl;
        }

        #region ICommand Members

        public void StartExecute()
        {
            this.undoModelContainer = (ModelContainer)modelControl.ModelContainer.Clone();
            modelControl.ModelContainer.AcceptVisitor(this);
        }

        public void Execute()
        {
        }

        public void EndExecute()
        {
        }

        public bool IsNeedModelUpdate
        {
            get { return true; }
        }

        public string Name
        {
            get { return "Выпрямить концы соединения"; }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            CommandFixSelectedShprossConnectorAngles clone = new CommandFixSelectedShprossConnectorAngles(modelControl);
            if (this.undoModelContainer != null)
                clone.undoModelContainer = (ModelContainer)this.undoModelContainer.Clone();
            if (this.redoModelContainer != null)
                clone.redoModelContainer = (ModelContainer)this.redoModelContainer.Clone();
            return clone;
        }

        #endregion

        #region IModelVisitor Members

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Model model)
        {
            //   throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Connector connector)
        {
            //  throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Shpross shpross)
        {
            //  throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Spacer spacer)
        {
            //  throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnectorStub stub)
        {
            //  throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnector shprossConnector)
        {
            if (shprossConnector.Selected)
            {
                int connectorIter = 0;
                foreach (IComponent innerComponent in shprossConnector.InnerComponents)
                {
                    if (innerComponent.GetType() == typeof(Connector))
                    {
                        Connector connector = innerComponent as Connector;
                        ModelVector shprossConnectorTangent = ModelCalculations.RotateVector(new ModelVector(1, 0), shprossConnector.Angle.Value);
                        Angle connectorAngle = ModelCalculations.GetAngleBetweenVectors(shprossConnectorTangent, connector.Primitives[0].GetTangent(0), false);
                        AngleInterval interval = shprossConnector.AllowedConnectorsAngles[connectorIter];
                        if (!interval.IsAngleInInterval(connectorAngle, shprossConnector.ConnectorAngleTolerance))
                        {
                            Angle angleToRotate = interval.MiddleAngle - connectorAngle;
                            connector.Rotate(angleToRotate.Value, connector.StartPoint);
                            if (connector.EndConnectedComponent != null)
                                connector.EndConnectedComponent.Rotate(angleToRotate.Value, connector.StartPoint);
                        }
                        connectorIter++;
                    }
                }
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerElement spacerElement)
        {
            //  throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerConnector spacerConnector)
        {
            // throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelCell modelCell)
        {
            //throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelContainer modelContainer)
        {
            foreach (IComponent innerComponent in modelContainer.AllInnerComponents)
            {
                innerComponent.AcceptVisitor(this);
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.StickConnector stickConnector)
        {
            //  throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SnapOnCap snapOnCap)
        {
            //   throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossEndToEndConnector endToEndConnector)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossChain shprossChain)
        {
    //        throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(SpacerContainer spacerContainer)
        {
           // throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(CrossConnector crossConnector)
        {
   //         throw new NotImplementedException();
        }

        #endregion

        #region ICommand Members

        private bool enabled = true;
        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        #endregion

        #region ICommand Members


        public bool IsNeedVisitorsUpdate
        {
            get { return true; }
        }

        #endregion
    }
}
